layui.define(['element', 'api', 'fast_config', 'fact_design', 'amis', 'utils'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'brms_design',
		$ = layui.jquery,
        api = layui.api,
        fast_config = layui.fast_config,
        fact_design = layui.fact_design,
        listeners = layui.listener,
        object_design = layui.object_design, 
        utils = layui.utils,
        amis = layui.amis
    ;

	var brms_design = function(opt) {
		this.option = opt;
	};

	function saveDesign(option){
        var grid_domain_vo_fields = option.grid_fact_design.option.grid.getData();
        var formObject = {};

        formObject["fields"] = grid_domain_vo_fields;

        let brmsFieldNames=[],brmsColumnNames=[];
        for(var field of grid_domain_vo_fields){
            brmsFieldNames.push(field["fieldName"]);
            brmsColumnNames.push(field["fieldComment"]);
        }

        const brmsFieldLength=brmsFieldNames.length,newBrmsFieldListLength=new Set(brmsFieldNames.map(item=>item)).size;
        const brmsColumnLength=brmsColumnNames.length,newBrmsColumnListLength=new Set(brmsColumnNames.map(item=>item)).size;

        if(brmsFieldLength>newBrmsFieldListLength){
            layer.alert('英文名不可重复，请调整后保存。');
        }else if(brmsColumnLength>newBrmsColumnListLength){
            layer.alert('名称不可重复，请调整后保存。');
        }else{
            var optionApi = {
                enableRootPath:true,
                type: "POST",
                url: fast_config.moudle.brms.saveApi.format({nodeid:option.treeNode.id}),
                data: JSON.stringify(formObject),
                contentType : 'application/json;charset=UTF-8',
                success: function(data){
                    layer.msg('操作成功');
                    if(option.callback && option.callback.reloadtree && typeof option.callback.reloadtree === 'function'){
                        option.callback.reloadtree(data);
                    }
                }
            };
            api.exec(optionApi)
        }
    }

    function appendElem(option){

        //debugger
        var id = option.treeNode.tId;
        var domainContainerId = 'domainContainer_' + id;
        var saveId = "saveDesignVo_" + id;

        var domainContainer = `
            <div id="${domainContainerId}">
                <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
                    <ul class="layui-tab-title domainTabs">
                        <li class="layui-this">基础字段</li>
                    </ul>
                    <div class="layui-tab-content" id="domainVoContent">
                        <div class="layui-tab-item item-pgGride layui-show" >
                            <div class="padding10 ">
                                <div id="${getDomainVoFields(option)}"></div>
                            </div>
                        </div>
                    </div>
            
            </div>
		`;
        $(option.elem).append(domainContainer);

        // <span class="buttonBox">
        //     <button id="${saveId}" type="button" class="layui-btn layui-btn-primary layui-btn-xs domainSave"> <span>保存</span></button>
        // </span>

        // $('#' + saveId).click(function(){
        //     saveDesign(option);
        // });
    }

    function getDomainVoFields(option){
        var id = option.treeNode.tId;
        return 'domainVoFields_' + id;
    }

    /**
	 * Button start loading
	 * */
    brms_design.prototype.render = function(opt) {
		
		var option = {
			elem: opt.elem,
            treeNode: opt.treeNode,
            callback: opt.callback,
            grid_fact_design: null
		}

        render(option);

        /**
         * 模块装配
         */
        listeners.registerToolbarListener("saveFactToolbarListener", (event, spanSchema, toolbrObj) => {
            //当data.id有值则会选中id节点，没有则只刷新
            saveDesign(option);
        })


        return new brms_design(option);
	}

    function render(option){

        appendElem(option);
        // var id = option.treeNode.tId;
        var modelJson = JSON.parse(option.treeNode.data.design);
        // debugger

        var initVoFields;
        if(modelJson !=null){
            initVoFields = modelJson.fields;
        }

        // grid;
        option.grid_fact_design = fact_design.render({
            elem: '#' + getDomainVoFields(option),
            initData: initVoFields,
            treeData: option.treeNode.data
        });
    }


    /**
     * 模块装配
     * 属性面板_单击树监听器
     */
     function configur(){
        listeners.registerContentPanelListener('fact_resource_content', (treeNode, target, option, pearBody, resource_tabs_obj) => {
            layui.brms_design.render({
                elem: utils.getTabElem(treeNode.tId),
                treeNode: treeNode,
                callback: {
                    reloadtree: function reloadCallback(data){
                        layui.resource_tree.selectedNode(data);
                    }
                }
            })
        });
    }

    configur();
    exports(MOD_NAME, new brms_design());

});
